﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using HCMServices.DTO;
using System.Linq;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace HCMServices.DAO
{
    [Table(Name = "TUKHOADICHVU")]
    public class KeyServiceDAO
    {
        [Column(Name = "MaTuKhoaDichVu", IsDbGenerated = true, IsPrimaryKey = true, DbType = "int")]
        public int MaTuKhoaDichVu { get; set; }

        [Column(Name = "TuKhoaDichVu", DbType = "nvarchar(64)")]
        public string TuKhoaDichVu { get; set; }

        [Column(Name = "maDichVu", DbType = "int")]
        public int MaDichVu { get; set; }

        private MyDataContext db;

        public KeyServiceDAO(string connectString)
        {
            db = new MyDataContext(connectString);
        }

        public List<KeyServiceDTO> Search(string strInput)
        {
            List<KeyServiceDTO> lstTuKhoaDichVuDTO = new List<KeyServiceDTO>();

            IList<TUKHOADICHVU> query = (from TUKHOADICHVU tkdv in db.KeyService
                                         select tkdv).ToList();

            foreach (TUKHOADICHVU tkdv in query)
            {
                if (strInput.Contains(tkdv.TuKhoaDichVu1))
                {
                    KeyServiceDTO temp = new KeyServiceDTO();
                    temp.KeyService = tkdv.TuKhoaDichVu1;
                    temp.KeyServiceId = tkdv.MaTuKhoaDichVu;
                    temp.ServiceId = (int)tkdv.MaDichVu;                // Note tkdv.maDichVu maybe null

                    lstTuKhoaDichVuDTO.Add(temp);
                }
            }

            return lstTuKhoaDichVuDTO;
        }

        public Dictionary<string, string> GetDictionaryOfKey()
        {
            Dictionary<string, string> result = new Dictionary<string, string>();

            var query = (from tkdv in db.KeyService
                         from dichvu in db.Service
                         where tkdv.MaDichVu == dichvu.MaDichVu
                         select new { tkdv.TuKhoaDichVu1, dichvu.TenDichVu }).ToList();

            foreach (var t in query)
            {
                if (t.TuKhoaDichVu1 != null && !result.ContainsKey(t.TuKhoaDichVu1))
                    result.Add(t.TuKhoaDichVu1, t.TenDichVu);
            }

            return result;
        }
    }
}
